<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../res/layui/css/layui.css">
		<link rel="stylesheet" href="../res/css/global.css">
		<script src="../res/layui/layui.js"></script>
		<script src="../js/jquery.min.js"></script>
		<script src="../js/axios-0.18.0.js"></script>
	</head>

	<body>
	<%--		<iframe  src="head.html" scrolling="no" width="100%" height="65px" ></iframe>--%>
	<jsp:include page="/WEB-INF/jsp/head.jsp"></jsp:include>

		<div class="main layui-clear">
			<div class=" layui-container fly-marginTop">
				<div class="fly-panel fly-panel-user" pad20="">
					<div class="layui-tab layui-tab-brief" lay-filter="user">
						<ul class="layui-tab-title">
							<li class="layui-this">登入</li>
							<li>
								<a href="${pageContext.servletContext.contextPath}/users/registerPage">注册</a>
							</li>
						</ul>
						<div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
							<div class="layui-tab-item layui-show">
								<div class="layui-form layui-form-pane">
									<form method="post" id="myForm">
										<div class="layui-form-item"> <label class="layui-form-label">用户名</label>
											<div class="layui-input-inline"> <input type="text" id="loginname" name="loginname" placeholder="请输入用户名"  autocomplete="off" class="layui-input"> </div>
										</div>
										<div class="layui-form-item"> <label class="layui-form-label">密码</label>
											<div class="layui-input-inline"> <input type="password" id="password" name="password"  placeholder="请输入密码"  autocomplete="off" class="layui-input"> </div>
										</div>

										 <button class="layui-btn" onclick="login()">用户登录</button> <br><br>
										 <button class="layui-btn" onclick="adminlogin()">管理登录</button>
<%--										<span style="padding-left:20px;">--%>
									</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>

<script>
	function login() {
		const form = document.getElementById('myForm');
		const formData = new FormData(form);
		console.log(formData);
		axios.post('/users/login', formData).then((res) => {
			console.log(res.data);
			if (res.data.code == 101) {
				sessionStorage.setItem("uid", ${user.uid});
				window.location.href = "${pageContext.servletContext.contextPath}/index";
			} else {
				alert(res.data.msg);
			}
		});

	}
		function adminlogin(){
			var loginName = $("#loginname").val();
			var password = $("#password").val();
			var identity = 2;
			// const formData = new FormData(document.getElementById("myForm"));
	console.log("1111111111111")
			$.ajax({
				url : "${pageContext.servletContext.contextPath}/users/admin",
				data : {"loginname":loginName, "password":password,"identity":identity},
				type : "post",
				success : function (msg) {

					// alert(msg);
					if (msg == 101){
						window.location.href = "${pageContext.servletContext.contextPath}/pages/users.html";
					}else if (msg == 102){
						alert("密码错误！")
					}else if (msg == 103){
						alert("用户名不存在！")
						return;
					}else if(msg == 104){
						alert("该账号已被禁用！")
					}
				}
			});
		}
</script>
<%--	function login(){--%>
<%--		var loginName = $("#loginname").val();--%>
<%--		var password = $("#password").val();--%>
<%--		var identity = 1;--%>
<%--		const formData = new FormData(document.getElementById("myForm"));--%>

<%--		$.ajax({--%>
<%--			url : "/users/login",--%>
<%--			data : {"loginname":loginName, "password":password,"identity":identity},--%>
<%--			type : "post",--%>
<%--		}).then((msg)=>{--%>
<%--			console.log(msg);--%>
<%--			if (msg == 101){--%>
<%--				sessionStorage.setItem("uid",${user.uid});--%>
<%--				window.location.href = "${pageContext.servletContext.contextPath}/index";--%>
<%--			}else {--%>
<%--				alert(msg.msg);--%>
<%--			}--%>
<%--		});--%>


<%--		&lt;%&ndash;axios.post(,formData).then((res)=>{&ndash;%&gt;--%>
<%--		&lt;%&ndash;	alert(res.data);&ndash;%&gt;--%>
<%--		&lt;%&ndash;});&ndash;%&gt;--%>
<%--	}--%>

<%--</script>--%>
</html>

